import logging
from typing import Union

from fastapi import APIRouter, Depends

from like.schema_base import PageInationResult
from like.http_base import unified_resp
from .schemas import (
    HpRulesListIn, HpRulesExportIn, HpRulesDetailIn, HpRulesAddIn, 
    HpRulesEditIn, HpRulesDelIn, HpRulesOut)
from .service import IHpRulesService, HpRulesService


logger = logging.getLogger(__name__)

router = APIRouter(prefix='/rules')


@router.get('/list', response_model=PageInationResult[HpRulesOut])
@unified_resp
async def rules_list(
        list_in: HpRulesListIn = Depends(),
        hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    """首页规则列表"""
    result = await hp_rules_service.list(list_in)
    result.file_name = "首页规则"
    return result


@router.get('/export', response_model=bytes)
@unified_resp
async def rules_export(export_in: HpRulesExportIn = Depends(), hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    return await hp_rules_service.export_excel(export_in)


@router.get('/detail')
@unified_resp
async def rules_detail(
        detail_in: HpRulesDetailIn = Depends(),
        hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    """首页规则详情"""
    return await hp_rules_service.detail(detail_in.id)


@router.post('/add')
@unified_resp
async def rules_add(
        add_in: HpRulesAddIn,
        hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    """首页规则新增"""
    return await hp_rules_service.add(add_in)


@router.post('/edit')
@unified_resp
async def rules_edit(
        edit_in: HpRulesEditIn,
        hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    """首页规则编辑"""
    return await hp_rules_service.edit(edit_in)


@router.post('/del')
@unified_resp
async def rules_del(
        del_in: HpRulesDelIn,
        hp_rules_service: IHpRulesService = Depends(HpRulesService.instance)):
    """首页规则删除"""
    return await hp_rules_service.delete(del_in.id)
